package studykana.adapters;

import java.util.ArrayList;

import studykana.models.Alphabet;
import studykana.utils.DisplayNextView;
import studykana.utils.Flip3dAnimation;
import toan.da.com.vn.R;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TimHinhAdapter extends BaseAdapter {

	private Activity activity;
	private ArrayList<Alphabet> listAlphabet;

	public TimHinhAdapter(Activity activity, ArrayList<Alphabet> listAlphabet) {
		this.activity = activity;
		this.listAlphabet = listAlphabet;
	}

	@Override
	public int getCount() {
		return listAlphabet.size();
	}

	@Override
	public Object getItem(int position) {
		return listAlphabet.get(position);
	}

	@Override
	public long getItemId(int position) {
		return listAlphabet.get(position).getId();
	}

	public static class ViewHolder {
		public ImageView imageFrontCard;
		public TextView tvBackCard;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder view;
		LayoutInflater inflator = activity.getLayoutInflater();
		if (convertView == null) {
			view = new ViewHolder();
			convertView = inflator.inflate(R.layout.tim_hinh_row, null);
			view.imageFrontCard = (ImageView) convertView
					.findViewById(R.id.image_card_front);
			view.tvBackCard = (TextView) convertView
					.findViewById(R.id.tv_card_back);
			convertView.setTag(view);
		} else {
			view = (ViewHolder) convertView.getTag();
		}

		Alphabet alphabet = new Alphabet(listAlphabet.get(position));

		if (alphabet.getDisplayText() == 0) {
			view.tvBackCard.setText(alphabet.getHiragana());
		} else if (alphabet.getDisplayText() == 1) {
			view.tvBackCard.setText(alphabet.getKatakana());
		} else if (alphabet.getDisplayText() == 2) {
			view.tvBackCard.setText(alphabet.getRomaji());
		}

		view.tvBackCard.setVisibility(View.GONE);
		return convertView;
	}
	
	public void setColorItem(View view, int color){
		((TextView) view.findViewById(R.id.tv_card_back)).setTextColor(color);
	}
	
	public void flipCard(View view, boolean isFirstImage){
		if (isFirstImage) {
			applyRotation(0, 90, isFirstImage, view);
		} else {
			applyRotation(0, -90, isFirstImage, view);
		}
	}

	private void applyRotation(float start, float end, boolean isFirstImage,
			View view) {

		ImageView imageFrontCard = (ImageView) view
				.findViewById(R.id.image_card_front);
		TextView tvBackCard = (TextView) view.findViewById(R.id.tv_card_back);
		// Find the center of image
		final float centerX = imageFrontCard.getWidth() / 2.0f;
		final float centerY = imageFrontCard.getHeight() / 2.0f;

		// Create a new 3D rotation with the supplied parameter
		// The animation listener is used to trigger the next animation
		final Flip3dAnimation rotation = new Flip3dAnimation(start, end,
				centerX, centerY);
		rotation.setDuration(100);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new AccelerateInterpolator());
		rotation.setAnimationListener(new DisplayNextView(isFirstImage,
				imageFrontCard, tvBackCard));

		if (isFirstImage) {
			imageFrontCard.startAnimation(rotation);
		} else {
			tvBackCard.startAnimation(rotation);
		}
	}

}
